<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 权限管理
 */

namespace Admin\Controller;
use Model\SigndaysModel;
use Model\SignhistoryModel;
use Model\UserModel;

class SignController extends BaseController{
    public $sModel;
    public $uModel;
    public function __construct(){
        parent::__construct();
        $this->sModel = new SignhistoryModel();
        $this->uModel = new UserModel();
    }
    //签到记录
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        if($sessionArr['name']){
            $uwhere['_string'] = "tel='{$sessionArr['name']}' or real_name='{$sessionArr['name']}'";
            $uRes = $this->uModel->findData($uwhere,'Id');
            $where['uid'] = $uRes['id'] ? $uRes['id'] : 0;
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $resList = $this->sModel->search($where,$page,$this->pernum);
        $totalNum = $this->sModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        $uidArr = [];
        foreach ($resList as $val){
            $uidArr[] = $val['uid'];
        }
        $sdRes = $uRes = [];
        if($uidArr){
            $uList = $this->uModel->selectData(['id'=>['in',$uidArr]]);
            foreach ($uList as $val){
                $uRes[$val['id']]['tname'] = $val['tel']?$val['tel']:$val['nick_name'];
                $uRes[$val['id']]['rname'] = $val['real_name'];
            }
            //连续签到天数
            $sdModel = new SigndaysModel();
            $sdList = $sdModel->selectData(['uid'=>['in',$uidArr]]);
            foreach ($sdList as $val){
                $sdRes[$val['uid']]['insist_days'] = $val['signdays'];
            }
        }

        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $this->assign('sdRes',$sdRes);
        $this->display('sign/index');
    }
}